package com.andreamerla.petslife.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import com.andreamerla.petslife.R;
import com.andreamerla.petslife.manager.AlertDialogManager;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;

import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 * User: a.merla
 * Date: 15/07/13
 * Time: 16.38
 */
public class Utils {
    public static final int GOOGLE_PLAY_SERVICES_REQUEST_CODE = 1;

    public static float round(float value, int decimals) {
        return Math.round(value * (float) Math.pow(10, decimals)) / (float) Math.pow(10, decimals);
    }

    public static double round(double value, int decimals) {
        return Math.round(value * Math.pow(10, decimals)) / Math.pow(10, decimals);
    }

    /**
     * This method converts dp unit to equivalent pixels, depending on device density.
     *
     * @param dp      A value in dp (density independent pixels) unit. Which we need to convert into pixels
     * @param context Context to get resources and device specific display metrics
     * @return A float value to represent px equivalent to dp depending on device density
     */
    public static float convertDpToPixel(float dp, Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return dp * (metrics.densityDpi / 160f);
    }

    /**
     * This method converts device specific pixels to density independent pixels.
     *
     * @param px      A value in px (pixels) unit. Which we need to convert into db
     * @param context Context to get resources and device specific display metrics
     * @return A float value to represent dp equivalent to px value
     */
    public static float convertPixelsToDp(float px, Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return px / (metrics.densityDpi / 160f);
    }

    public static void hideKeyboard(Context context, View view) {
        // Hide soft keyboard
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    public static String getLogTag(Class<?> cls) {
        return "PetsLife-" + cls.getName();
    }

    public static boolean checkGooglePlayServices(Activity activity) {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (resultCode == ConnectionResult.SERVICE_MISSING ||
                    resultCode == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED ||
                    resultCode == ConnectionResult.SERVICE_DISABLED) {
                Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, activity, GOOGLE_PLAY_SERVICES_REQUEST_CODE);
                dialog.show();
            } else // if (resultCode == ConnectionResult.SERVICE_INVALID)
                AlertDialogManager.showAlertDialog(activity, activity.getString(R.string.error), activity.getString(R.string.error_play_services_missing));
            return false;
        }
        return true;
    }

    public static LatLngBounds getLatLngBounds(Collection<Marker> markerList) {
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for (Marker m : markerList)
            builder.include(m.getPosition());

        return builder.build();
    }
}
